package com.kingsoft.dc.khaos.extender.auth;

import com.alibaba.fastjson.JSON;
import com.kingsoft.dc.khaos.extender.meta.api.AuthRequest;
import com.kingsoft.dc.khaos.extender.meta.api.DmApiUtils;
import com.kingsoft.dc.khaos.extender.meta.api.DmAuthResult;
import com.kingsoft.dc.khaos.extender.meta.api.DmRequest;
import com.kingsoft.dc.khaos.extender.meta.impl.DmAuthImpl;
import com.kingsoft.dc.khaos.extender.meta.model.AuthTable;
import com.kingsoft.dc.khaos.extender.meta.model.TableAuthParams;
import com.kingsoft.dc.khaos.extender.meta.model.auth.DmAuth;
import com.kingsoft.dc.khaos.extender.model.ParamConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jing on 19/8/10.
 */
public class DmAuthentication {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 检查权限
     * @param tableList
     * @return
     */
    public boolean checkPriv(List<AuthTable> tableList, ParamConfig paramConfig){
        if (tableList == null || tableList.isEmpty()) {
            return false;
        }
        String dsType = paramConfig.getDsType();
        DmRequest request = buildRequest(paramConfig, tableList);
        DmAuth authDetail = DmAuthImpl.getProjectAuthDetail(request);
        return  authDetail.isTotalPrivs();
//            AuthDetailInfo detailInfo = authDetail.getAuthDetailInfo();
//            int projectId = paramConfig.getJobProjectId();
//            logger.error("此项目没有权限访问数据表,项目:[{}],数据源类型:[{}],数据表名称:[{}.{}],被拒绝的权限:[{}]", projectId, dsType, detailInfo.getDbName(), detailInfo.getTableName(), detailInfo.formatDetailInfo());
//            return false;
//        } else {
//            return true;
//        }
    }
    public DmAuthResult checkPrivWithInfo(List<AuthTable> tableList, ParamConfig paramConfig){
        if (tableList == null || tableList.isEmpty()) {
            return null;
        }
        String dsType = paramConfig.getDsType();
        DmRequest request = buildRequest(paramConfig, tableList);
//        DmAuth authDetail = DmAuthImpl.getProjectAuthDetail(request);
        DmAuthResult userAuth = DmApiUtils.getProjectAuthDetail(request);
        return  userAuth;
    }

    /**
     * 检查权限
     * @param table 需要鉴权的表
     * @return
     */
    public boolean checkPriv(AuthTable table, ParamConfig paramConfig) {
        List<AuthTable> tableList = new ArrayList<>();
        tableList.add(table);
        return checkPriv(tableList, paramConfig);
    }


    /**
     * 检查权限
     * @param dbName 需要鉴权的库
     * @param tableName 需要鉴权的表
     * @param privList 需要鉴定的权限列表
     * @return
     */
    public boolean checkPriv(String dbName, String tableName, List<String> privList, String config) {
        ParamConfig paramConfig = JSON.parseObject(config, ParamConfig.class);
        List<AuthTable> tableList = new ArrayList<>();
        tableList.add(createTableAuth(dbName, tableName, privList.toArray(new String[privList.size()])));
        return checkPriv(tableList, paramConfig);
    }

    public AuthTable createTableAuth(String dbName, String tableName, String[] privs) {
        AuthTable tableAuth = new AuthTable();
        if (dbName == null || tableName == null) {
            return null;
        } else {
            tableAuth.setDbName(dbName);
            tableAuth.setTblName(tableName);
            tableAuth.setPrivSet(privs);
        }
        return tableAuth;
    }

    public DmRequest buildRequest(ParamConfig paramConfig, List<AuthTable> authTables) {
        return buildRightQueryRequest(paramConfig, authTables);
    }

    /**
     * 获取
     * @param paramConfig
     */
    public static DmRequest buildRightQueryRequest(ParamConfig paramConfig, List<AuthTable> authTables) {
        DmRequest request = new DmRequest();
        AuthRequest authRequest = new AuthRequest();
        authRequest.setAuthUrl(paramConfig.getAuthUrl());
        authRequest.setAuthProjectId(paramConfig.getJobProjectId());
        request.setUrl(paramConfig.getUrl());
        request.setAuthRequest(authRequest);
        TableAuthParams authParams = new TableAuthParams();
        authParams.buildAuthParams(
                paramConfig.getRunEnv(),
                paramConfig.getJobProjectId(),
                paramConfig.getDsId(),
                authTables);
        authParams.setJobProjectName(paramConfig.getJobProjectName());
        authParams.setDsName(paramConfig.getDsName());
        request.setTableAuthParams(authParams);
        return request;
    }

    public static void main(String[] args) {
        ParamConfig paramConfig = new ParamConfig();
        paramConfig.setDsType("");//数据源类型
        paramConfig.setJobProjectId(1);//项目ID
        paramConfig.setRunEnv("TEST");//运行环境
        paramConfig.setAuthUrl("");//认证URL
        paramConfig.setUrl("");//访问的服务URL
        DmAuthentication authentication = new DmAuthentication();
        AuthTable authTable = authentication.createTableAuth("", "", new String[]{"INSERT"});
        boolean checkResult = authentication.checkPriv(authTable, paramConfig);
    }
}
